<script setup lang="ts">
import { ref } from 'vue'
import { uploadMusic } from '@/api/source'
import { ElMessage } from 'element-plus'
import type { UploadFile } from 'element-plus'

const emit = defineEmits<{
  (e: 'upload-success'): void
}>()

// 上传表单数据
const uploadForm = ref({
  title: '',
  artist: '',
  musicFile: null as File | null,
  coverFile: null as File | null,
  lyricFile: null as File | null
})

// 上传状态
const uploading = ref(false)

// 处理文件选择
const handleFileChange = (file: UploadFile, type: 'music' | 'cover' | 'lyric') => {
  if (file.raw) {
    uploadForm.value[`${type}File`] = file.raw
  }
}

// 处理上传
const handleUpload = async () => {
  // 验证必填项
  if (!uploadForm.value.title || !uploadForm.value.artist || !uploadForm.value.musicFile) {
    ElMessage.warning('请填写必要信息并选择音乐文件')
    return
  }

  uploading.value = true
  try {
    const formData = new FormData()
    formData.append('title', uploadForm.value.title)
    formData.append('artist', uploadForm.value.artist)
    formData.append('musicFile', uploadForm.value.musicFile)
    
    if (uploadForm.value.coverFile) {
      formData.append('coverFile', uploadForm.value.coverFile)
    }
    if (uploadForm.value.lyricFile) {
      formData.append('lyricFile', uploadForm.value.lyricFile)
    }

    const result = await uploadMusic(formData)
    if (result.success) {
      ElMessage.success('上传成功')
      // 重置表单
      uploadForm.value = {
        title: '',
        artist: '',
        musicFile: null,
        coverFile: null,
        lyricFile: null
      }
      // 通知父组件刷新列表
      emit('upload-success')
    } else {
      ElMessage.error(result.message)
    }
  } finally {
    uploading.value = false
  }
}
</script>

<template>
  <section class="upload-section">
    
    <!-- -------------------禁用遮罩层------------------- -->
    <!-- 
    <div class="disabled-mask">
      <div class="disabled-content">
        <i class="el-icon-lock"></i>
        <h3>测试期间关闭</h3>
        <p>暂时关闭，数据库容量有限</p>
      </div>
    </div>
    -->
    <!-- -------------------禁用遮罩层------------------- -->
    
    <h2>上传音乐</h2>
    <div class="upload-form">
      <el-form :model="uploadForm" label-width="120px" label-position="left">
        <el-form-item label="歌曲名称" required>
          <el-input v-model="uploadForm.title" placeholder="请输入歌曲名称" />
        </el-form-item>
        
        <el-form-item label="歌手" required>
          <el-input v-model="uploadForm.artist" placeholder="请输入歌手名称" />
        </el-form-item>
        
        <el-form-item label="音乐文件" required>
          <el-upload
            class="upload-demo"
            action="#"
            :auto-upload="false"
            :limit="1"
            :on-exceed="() => ElMessage.warning('只能上传一个音乐文件')"
            :show-file-list="true"
            :on-change="(file) => handleFileChange(file, 'music')"
            accept="audio/*"
          >
            <el-button type="primary">选择音乐文件</el-button>
            <template #tip>
              <div class="upload-tip">支持 MP3、WAV 等音频格式</div>
            </template>
          </el-upload>
        </el-form-item>
        
        <el-form-item label="封面图片">
          <el-upload
            class="upload-demo"
            action="#"
            :auto-upload="false"
            :limit="1"
            :on-exceed="() => ElMessage.warning('只能上传一张封面图片')"
            :show-file-list="true"
            :on-change="(file) => handleFileChange(file, 'cover')"
            accept="image/*"
          >
            <el-button>选择封面图片</el-button>
            <template #tip>
              <div class="upload-tip">建议尺寸 500x500，支持 JPG、PNG 格式</div>
            </template>
          </el-upload>
        </el-form-item>
        
        <el-form-item label="歌词文件">
          <el-upload
            class="upload-demo"
            action="#"
            :auto-upload="false"
            :limit="1"
            :on-exceed="() => ElMessage.warning('只能上传一个歌词文件')"
            :show-file-list="true"
            :on-change="(file) => handleFileChange(file, 'lyric')"
            accept=".lrc"
          >
            <el-button>选择歌词文件</el-button>
            <template #tip>
              <div class="upload-tip">仅支持 LRC 格式的歌词文件</div>
            </template>
          </el-upload>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleUpload" :loading="uploading">
            {{ uploading ? '上传中...' : '开始上传' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </section>
</template>

<style scoped lang="scss">
.upload-section {
  position: relative;
  background-color: color-mix(in srgb, var(--bgc-color-2) 40%, transparent);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 24px;
  margin-bottom: 20px;
  overflow: hidden;

  // 顶部装饰线
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(
      90deg,
      transparent 0%,
      color-mix(in srgb, var(--primary-color) 10%, transparent) 20%,
      color-mix(in srgb, var(--primary-color) 20%, transparent) 50%,
      color-mix(in srgb, var(--primary-color) 10%, transparent) 80%,
      transparent 100%
    );
  }

  h2 {
    color: var(--primary-color);
    font-size: 1.8em;
    margin-bottom: 28px;
    font-weight: 600;
    letter-spacing: 0.5px;
    background: linear-gradient(to right, var(--primary-color), var(--theme-color));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    position: relative;
    display: inline-block;

    &::after {
      content: '';
      position: absolute;
      bottom: -8px;
      left: 0;
      width: 60px;
      height: 3px;
      background: linear-gradient(to right, var(--theme-color), transparent);
      border-radius: 2px;
    }
  }

  .upload-form {
    max-width: 600px;
    padding: 24px;
    border-radius: 12px;

    .upload-tip {
      color: rgba(var(--primary-color), 0.6);
      font-size: 12px;
      margin-top: 8px;
      line-height: 1.4;
    }

    :deep(.el-upload-list) {
      margin-top: 8px;
      
      .el-upload-list__item {
        background: rgba(var(--bgc-color-2), 0.3);
        border: 1px solid rgba(var(--theme-color), 0.2);
        border-radius: 4px;
        padding: 8px 12px;
        margin-bottom: 8px;
        
        &:hover {
          background: rgba(var(--theme-color), 0.15);
        }
        
        .el-upload-list__item-name {
          color: var(--primary-color);
        }
      }
    }

    :deep(.el-form-item) {
      margin-bottom: 24px;
      
      &:last-child {
        margin-bottom: 0;
      }

      .el-form-item__content {
        line-height: 32px;
      }
    }

    :deep(.el-form-item__label) {
      color: rgba(var(--primary-color), 0.9);
      font-weight: 500;
      line-height: 32px;
      padding-right: 12px;
      white-space: nowrap;
      justify-content: flex-start;
      text-align: left;
    }

    :deep(.el-form-item.is-required .el-form-item__label:before) {
      color: #f56c6c;
      margin-right: 4px;
    }

    :deep(.el-input__wrapper) {
      background: color-mix(in srgb, var(--bgc-color-1) 95%, transparent);
      border: 1px solid color-mix(in srgb, var(--primary-color) 10%, transparent);
      box-shadow: none;

      &.is-focus {
        border-color: var(--theme-color);
        box-shadow: 0 0 0 2px color-mix(in srgb, var(--theme-color) 20%, transparent);
      }

      .el-input__inner {
        color: var(--primary-color);
        
        &::placeholder {
          color: color-mix(in srgb, var(--primary-color) 50%, transparent);
        }
      }
    }

    :deep(.el-upload) {
      width: 100%;
      
      .el-button {
        width: 100%;
        background: color-mix(in srgb, var(--bgc-color-1) 95%, transparent);
        border: 1px solid color-mix(in srgb, var(--primary-color) 10%, transparent);
        color: var(--primary-color);
        height: 40px;
        transition: all 0.3s ease;

        &:hover {
          background: color-mix(in srgb, var(--theme-color) 15%, transparent);
          border-color: var(--theme-color);
          transform: translateY(-2px);
        }
      }
    }

    :deep(.el-upload-list) {
      color: rgba(var(--primary-color), 0.9);
    }
    
    :deep(.el-upload-list__item) {
      background: color-mix(in srgb, var(--bgc-color-1) 95%, transparent);
      border: 1px solid color-mix(in srgb, var(--primary-color) 10%, transparent);
      border-radius: 4px;
      padding: 8px 12px;
      margin-bottom: 8px;
      
      &:hover {
        background: color-mix(in srgb, var(--theme-color) 15%, transparent);
      }
      
      .el-upload-list__item-name {
        color: var(--primary-color);
      }
    }

    :deep(.el-form-item:last-child) {
      margin-bottom: 0;
      
      .el-button {
        background: var(--theme-gradient);
        border: none;
        color: var(--bgc-color-1);
        padding: 12px 24px;
        height: auto;
        font-weight: 500;
        width: 100%;
        transition: all 0.3s ease;
        
        &:hover {
          background: linear-gradient(145deg, var(--secondary-color), var(--theme-color));
          transform: translateY(-2px);
          box-shadow: 0 4px 12px var(--theme-shadow);
        }

        &.is-loading {
          opacity: 0.8;
          transform: none;
          box-shadow: none;
        }
      }
    }
  }

  // -------------------禁用遮罩层-------------------
  .disabled-mask {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    border-radius: 20px;
  }

  .disabled-content {
    text-align: center;
    color: var(--primary-color);
    
    i {
      font-size: 48px;
      margin-bottom: 16px;
      color: var(--theme-color);
    }
    
    h3 {
      font-size: 24px;
      margin-bottom: 8px;
      background: linear-gradient(to right, var(--primary-color), var(--theme-color));
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
    
    p {
      color: rgba(var(--primary-color), 0.7);
      font-size: 16px;
    }
  }
  // -------------------禁用遮罩层-------------------

}
</style> 